package cn.jetpiece.cloud.joss.app.modular.basics.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.jetpiece.cloud.joss.api.basic.ConfigApi;
import cn.jetpiece.cloud.joss.api.basic.model.params.CreateConfigParam;
import cn.jetpiece.cloud.joss.api.basic.model.params.UpdateConfigParam;
import cn.jetpiece.cloud.joss.api.basic.model.query.ConfigQuery;
import cn.jetpiece.cloud.joss.api.basic.model.result.ConfigResult;
import cn.jetpiece.cloud.joss.app.config.properties.OssProperties;
import cn.jetpiece.cloud.joss.app.modular.basics.entity.Config;
import cn.jetpiece.cloud.joss.app.modular.basics.service.ConfigService;
import cn.jetpiece.cloud.model.contants.ResponseMsgConstants;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 对象存储配置控制器
 *
 * @author XuJZ
 * @since 2020-09-05 15:41:52
 */
@Slf4j
@RestController
public class ConfigController implements ConfigApi {

    @Autowired
    private ConfigService configService;

    @Override
    public Result<String> add(CreateConfigParam param) {
        if (configService.add(param)) {
            return Result.OK(ResponseMsgConstants.CREATE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.CREATE_ERROR);
        }
    }

    @Override
    public Result<String> update(UpdateConfigParam param) {
        if (configService.update(param)) {
            return Result.OK(ResponseMsgConstants.UPDATE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.UPDATE_ERROR);
        }
    }

    @Override
    public Result<String> delete(String id) {
        if (configService.delete(id)) {
            return Result.OK(ResponseMsgConstants.DELETE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.DELETE_ERROR);
        }
    }

    @Override
    public Result<ConfigResult> get(String id) {
        Config bean = configService.getById(id);
        if (bean == null) {
            return Result.OK();
        }
        ConfigResult result = new ConfigResult();
        BeanUtil.copyProperties(bean, result);
        return Result.OK(result);
    }

    @Override
    public Result<PageResult<ConfigResult>> page(ConfigQuery query) {
        return Result.OK(configService.page(query));
    }

    @Override
    public String getBaseUrlPrefix() {
        return OssProperties.getInstance().getBasePreviewUrl();
    }

}
